//
//  UIImageView+Extension.swift
//  HomeSchoolDemo
//
//  Created by rimi on 16/10/12.
//  Copyright © 2016年 黄华强. All rights reserved.
//

import UIKit

extension UIImageView {
    
    /// 隔离网络三方的网络下载方法
    ///
    /// - parameter urlStr:           图片地址
    /// - parameter placeholderImage: 占位图名称
    /// - parameter size:             头像视图大小
    /// - parameter bgColor:          背景色
    func xx_setImage(urlStr:String,placeholderImage:String,size:CGSize,bgColor:UIColor = .white) {
        //先处理占位图
        var placeImage = UIImage(named: placeholderImage)
        //重新绘制
        placeImage = placeImage?.reverImage(size: size, bgColor: bgColor)
        
        //转换为request
        guard let url = URL(string: urlStr) else {
            image = placeImage
            return
        }
        
        let request = URLRequest(url: url)
        self.setImageWith(request, placeholderImage: placeImage, success: {[weak self] (_, _, netImage) in
            self?.image = netImage.reverImage(size: size, bgColor: bgColor)
            })
    }
}
